
#import "BaseViewController.h"


@interface BaseViewController ()<UISearchBarDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    if(self.navigationController.viewControllers.count>0)
    {
        if(self!=self.navigationController.viewControllers[0])
        {
            //左上角返回按钮
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(popVc)];
        }
    }
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    //适配ios10
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (self.isHidenBackgroundImage) {
        UIImageView *backgroundImage=[UIImageView new];
        [self.view addSubview:backgroundImage];
        [backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
        backgroundImage.image=[UIImage imageNamed:@"beijing"];
        
        UIView *titleView=[UIView new];
        [self.view addSubview:titleView];
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(40);
            make.height.mas_equalTo(44);
        }];
        
        UILabel *titleLable=[UILabel new];
        [titleView addSubview:titleLable];
        [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(50);
            make.right.mas_equalTo(-50);
            make.top.bottom.mas_equalTo(0);
        }];
        titleLable.text=self.navigationItem.title;
        titleLable.textColor=[UIColor whiteColor];
        titleLable.font=[UIFont fontWithName:@"Helvetica-Bold" size:autoScaleW(38)];
        titleLable.textAlignment=NSTextAlignmentCenter;
        
        self.rightBarButton=[UIButton new];
        [titleView addSubview:self.rightBarButton];
        [self.rightBarButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.width.height.mas_equalTo(36*_mainWidth);
            make.right.mas_equalTo(-35*_mainWidth);
        }];
        self.rightBarButton.hidden=YES;
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    if (self.isHidenDog) {
        UIImage *navImg =[UIImage imageNamed:@"dongwu"];
        navImg = [navImg resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
       [self.navigationController.navigationBar setBackgroundImage:navImg forBarMetrics:UIBarMetricsDefault];
    }else{
        UIImage *navImg =[UIImage imageNamed:@"wudongwu"];
        navImg = [navImg resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
       [self.navigationController.navigationBar setBackgroundImage:navImg forBarMetrics:UIBarMetricsDefault];
    }
}


#pragma mark——————————收到内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.view endEditing:YES];
}


//返回上一个界面
- (void)popVc
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)dealloc
{
    
    [self.view endEditing:YES];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    //清空并重置数据
    [[CDPMonitorKeyboard defaultMonitorKeyboard] clearAll];
   
}


// cell点击变色
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    //设置(Highlight)高亮下的颜色
    [cell setBackgroundColor:rgba(220, 220, 220, 1)];
}

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    //设置(Nomal)正常状态下的颜色
    [cell setBackgroundColor:[UIColor whiteColor]];
}



#pragma mark————————程序即将离开页面
- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    [self.view endEditing:YES];
    
}
- (void)getWithUrl:(NSString *_Nullable)urlString param:(NSDictionary *_Nullable)param tableView:(UITableView *_Nonnull)tableView success:(SuccessBlocka _Nullable )success failure:(FailureBlock _Nullable )failure{
    
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[UIApplication sharedApplication].keyWindow];
    if (!hud) {
         [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    }
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *urlStr=[NSString stringWithFormat:@"%@%@",_addressUrl,urlString];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   
    [manager GET:urlStr parameters:param headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];

        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
     
        baseModel *model=[baseModel mj_objectWithKeyValues:responseObject];
//        if (model.success) {
             success(model.datas);
//        }else{
//            NSLog(@"%@",model.errmsg);
//            [ZQTools svpInfo:model.errmsg];
//        }
//
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
        
        NSLog(@"%@",error);
        [ZQTools svpInfo:@"访问失败"];
    }];
}
- (void)PostWithUrl:(NSString *_Nullable)urlString param:(NSDictionary *_Nullable)param tableView:(UITableView *_Nonnull)tableView success:(SuccessBlocka _Nullable )success failure:(FailureBlock _Nullable )failure{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[UIApplication sharedApplication].keyWindow];
    if (!hud) {
         [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    }
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *urlStr=[NSString stringWithFormat:@"%@%@",_addressUrl,urlString];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   
    [manager POST:urlStr parameters:param headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];

        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
     
        baseModel *model=[baseModel mj_objectWithKeyValues:responseObject];
//        if (model.success) {
             success(model.datas);
//        }else{
//            NSLog(@"%@",model.errmsg);
//            [ZQTools svpInfo:model.errmsg];
//        }
//
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
        
        NSLog(@"%@",error);
        [ZQTools svpInfo:@"访问失败"];
    }];
    
}
- (void)PutWithUrl:(NSString *_Nullable)urlString param:(NSDictionary *_Nullable)param tableView:(UITableView *_Nonnull)tableView success:(SuccessBlocka _Nullable )success failure:(FailureBlock _Nullable )failure{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[UIApplication sharedApplication].keyWindow];
    if (!hud) {
         [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    }
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *urlStr=[NSString stringWithFormat:@"%@%@",_addressUrl,urlString];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   
    
    [manager PUT:urlStr parameters:param headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];

        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
     
        baseModel *model=[baseModel mj_objectWithKeyValues:responseObject];
//        if (model.success) {
             success(model.datas);
//        }else{
//            NSLog(@"%@",model.errmsg);
//            [ZQTools svpInfo:model.errmsg];
//        }
//
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
        
        NSLog(@"%@",error);
        [ZQTools svpInfo:@"访问失败"];
    }];
    
}
- (void)DeleteWithUrl:(NSString *_Nullable)urlString param:(NSDictionary *_Nullable)param tableView:(UITableView *_Nonnull)tableView success:(SuccessBlocka _Nullable )success failure:(FailureBlock _Nullable )failure{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[UIApplication sharedApplication].keyWindow];
    if (!hud) {
         [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    }
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *urlStr=[NSString stringWithFormat:@"%@%@",_addressUrl,urlString];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   
    
    [manager DELETE:urlStr parameters:param headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];

        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
     
        baseModel *model=[baseModel mj_objectWithKeyValues:responseObject];
//        if (model.success) {
             success(model.datas);
//        }else{
//            NSLog(@"%@",model.errmsg);
//            [ZQTools svpInfo:model.errmsg];
//        }
//
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        if (tableView!=nil) {
            [tableView.mj_footer endRefreshing];
            [tableView.mj_header endRefreshing];
        }
        
        NSLog(@"%@",error);
        [ZQTools svpInfo:@"访问失败"];
    }];
    
}
@end
